<?php
/**
 * User: 五哥派
 * Date: 2024/6/30
 * Tel(同WX): 17335592719
 */

namespace app\admin\controller\store;

use app\admin\controller\Base;

use app\admin\model\store\Goods as GoodsModel;
use app\admin\model\store\GoodsSku as GoodsSkuModel;
use app\admin\model\store\Category as CategoryModel;

use app\admin\model\store\Specs as SpecsModel;
use app\admin\model\store\SpecsValue as SpecsValueModel;

use app\admin\service\Goods as GoodsService;

class Goods extends Base{

    protected $model;
    protected $mGoodsSkuModel;
    protected $mCategoryModel;
    protected $mSpecsModel;
    protected $mSpecsValueModel;

    public function __construct(GoodsModel $model, GoodsSkuModel $mGoodsSkuModel,
                                CategoryModel $categoryModel, SpecsModel $mSpecsModel,
                                SpecsValueModel $mSpecsValueModel){
        parent::__construct();

        $this->model = $model;
        $this->mGoodsSkuModel = $mGoodsSkuModel;
        $this->mCategoryModel = $categoryModel;
        $this->mSpecsModel = $mSpecsModel;
        $this->mSpecsValueModel = $mSpecsValueModel;

    }

    public function lst(){
        return $this->fetch();
    }
    public function lstajax(){
        //$name = input("get.name")?input("get.name"):'';
        //$where['user_id'] = $id;
        //$where['url'] = array('like','%'.$name.'%');
        //$where['delete_time'] = array('>', 0);
        //$limit = input("get.limit");
        //$page = input("get.page");
        //$count = $this->model->total($where);
        //$result = $this->model->paginate1($limit, $page, $where);
        //$list = $this->model->getList($where);
        //$result = $list->toArray();
        //return json($result);
        //$code, $msg='error', $count = 0, $data=[]
        $list = $this->model->getList();
        $result = $list->toArray();
        return showLayui($result['total'], $result['data']);
    }

    public function add(){

        if(request()->isPost()){
            $data = request()->param();
            if(intval($data['specs_type']) == 2){
                $goods_specs_data = [
                    'spec' => 2,
                    'add_spec_arr' => $data['add_spec_arr'],
                    'skus' => $data['skus'],
                ];
                $data['goods_specs_data'] = json_encode($goods_specs_data, JSON_UNESCAPED_UNICODE );
            }

            $res = (new GoodsService())->insertData($data);
            if(!$res) {
                return error('保存失败!');
            }
            return success('保存成功!');
        }

        $parentData = $this->mCategoryModel->getTree(['status' => 1]);
        $this->assign('parentData', $parentData);

        return $this->fetch();
    }
    public function edit(){
        if(request()->isPost()){
            $data = request()->param();
            if(intval($data['specs_type']) == 2){
                $goods_specs_data = [
                    'spec' => 2,
                    'add_spec_arr' => $data['add_spec_arr'],
                    'skus' => $data['skus']
                ];
                $data['goods_specs_data'] = json_encode($goods_specs_data, JSON_UNESCAPED_UNICODE );
            }
            $res = (new GoodsService())->updateData($data);
            if(!$res) {
                return error('保存失败!');
            }
            return success('保存成功!');
        }

        $id = input('id');
        $data = $this->model->findDataByID($id);
        $this->assign('data',$data);

        if(empty($data['carousel_image'])){
            $carousel_image = [];
            $this->assign('carousel_image_arr',$carousel_image);
        }else{
            $carousel_image = explode(',',$data['carousel_image']);
            $this->assign('carousel_image_arr',$carousel_image);
        }

        $parentData = $this->mCategoryModel->getTree(['status' => 1]);
        $children = $this->mCategoryModel->getChildren($data['category_id'],['status' => 1]);
        $this->assign(array(
            'parentData' => $parentData,
            'children' => $children,
        ));

        return $this->fetch();
    }


    public function del(){
        $id = input('id');
        //删除
        $res = $this->model->delDataByWhere(['id' => $id]);
        if(!$res) {
            return error('保存失败!');
        }
        return success('保存成功!');
    }

    public function change(){
        $id = input('get.id');
        $field = input('get.field');
        $value = input('get.value');
        $res = $this->model->saveData(['id'=>$id, $field=>$value],true);
        if(!$res) {
            return error('设置失败!');
        }
        return success('设置成功!');
    }

    public function switchHot(){
        $id = input('get.id');
        $is_hot = input('get.is_hot');
        $res = $this->model->saveData(['id' => $id, 'is_hot' => $is_hot],true);
        if(!$res) {
            return error('设置失败!');
        }
        return success('设置成功!');
    }

    public function switchStatus(){
        $id = input('get.id');
        $status = input('get.status');
        $res = $this->model->saveData(['id'=>$id,'status'=>$status],true);
        if(!$res) {
            return error('设置失败!');
        }
        return success('设置成功!');
    }



}




